# Author:lzhan  Date:2020/3/17
# import lxml.html as lx

# lxml 包   html 模块  etree 方法
from lxml.html import etree

# 1. 读取文件  2. 把文件转化为可查找的对象

# xml_books是一个树形的结构，他是树根
# xml_books
xml_books=etree.parse('data/books.xml')

# xpath 寻址
# books 是一个列表
books=xml_books.xpath('//book[@category="cooking"]')
list_books=[]
for book in books:
    dict={}
    title=book.xpath('./title')[0].text
    lang=book.xpath('./title/@lang')[0]
    author=book.xpath('./author')[0].text
    price=book.xpath('./price')[0].text
    year=book.xpath('./year')[0].text
    dict["title"]=title
    dict["lang"]=lang
    dict["author"]=author
    dict["price"]=price
    dict["year"]=year
    list_books.append(dict)
print(list_books)



